package com.ezhou.hy.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;



/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>CopyRight:Copyright (c) 2008.12 </p>
 * <p>Company: </p>
 * @author 
 * @version 1.0
 */
public class Resources extends Object{
	
	private static ClassLoader defaultClassLoader;
	
	private Resources(){
		
	}

	/**
	 * @return the defaultClassLoader
	 */
	public static ClassLoader getDefaultClassLoader() {
		return defaultClassLoader;
	}

	/**
	 * @param defaultClassLoader the defaultClassLoader to set
	 */
	public static void setDefaultClassLoader(ClassLoader defaultClassLoader) {
		Resources.defaultClassLoader = defaultClassLoader;
	}
	
	/**
	 * 
	 * @param resource
	 * @return
	 * @throws IOException
	 */
	public static InputStream getResourceAsStream(String resource)throws IOException{
		return getResourceAsStream(getClassLoader(),resource);
	}
	
	/**
	 * 
	 * @param loader
	 * @param resource
	 * @return
	 * @throws IOException
	 */
	public static InputStream getResourceAsStream(ClassLoader loader, String resource)throws IOException{
		InputStream in = null;
		if(loader != null) in = loader.getResourceAsStream(resource);
		if(in == null) in = ClassLoader.getSystemResourceAsStream(resource);
		if(in == null) throw new IOException("Could Not Find Resource"+resource);
		
		return in;
	}
	/**
	 * 
	 * @param resource
	 * @return
	 * @throws IOException
	 */
	public static Reader getResourceAsReader(String resource) throws IOException{
		return new InputStreamReader(getResourceAsStream(resource));
	}
	
	/**
	 * 
	 * @param loader
	 * @param resource
	 * @return
	 * @throws IOException
	 */
	public static Reader getResourceAsReader(ClassLoader loader,String resource) throws IOException{
		return new InputStreamReader(getResourceAsStream(loader, resource));
	}
	
	/**
	 * 
	 * @param resource
	 * @return
	 * @throws IOException
	 */
	public static Properties getResourceAsProperties(String resource) throws IOException{
		Properties props = new Properties();
		InputStream in = null;
		String propfile = resource;
		
		in = getResourceAsStream(propfile);
		props.load(in);
		in.close();
		
		return props;
	}
	
	/**
	 * 
	 * @param loader
	 * @param resource
	 * @return
	 * @throws IOException
	 */
	public static Properties getResourceAsProperties(ClassLoader loader,String resource) throws IOException{
		Properties props = new Properties();
		InputStream in = null;
		
		in = getResourceAsStream(loader, resource);
		props.load(in);
		in.close();
		
		return props;
		
	}
	
	/**
	 * 
	 * @param className
	 * @return
	 * @throws ClassNotFoundException
	 */
	public static Class classForName(String className)throws ClassNotFoundException{
		Class clazz = null;
		try{
			clazz = getClassLoader().loadClass(className);
		}catch(Exception e){
			//do Nothing
		}
		if(clazz == null){
			clazz = Class.forName(className);
		}
		return clazz;
	}
	
	/**
	 * 
	 * @param className
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static Object instantiate(String className)
	    throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		return instantiate(classForName(className));
	}
	
	/**
	 * 
	 * @param clazz
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static Object instantiate(Class clazz)
	    throws ClassNotFoundException, InstantiationException, IllegalAccessException{
		return clazz.newInstance();
	}
	
	/**
	 * 
	 * @return
	 */
	private static ClassLoader getClassLoader(){
		if(defaultClassLoader != null){
			return defaultClassLoader;
		}else{
			return Thread.currentThread().getContextClassLoader();
		}
	}

}
